package main

import (
	"time"
)

type registers interface {
	CheckAndPopup()
	DoRegister(job *CronJob)
	Remove(name string)
}

type register struct {
	scheduleTable  map[string] *CronJob
	jobChannel chan<- *CronJob
}

func NewRegister(ch chan<- *CronJob) registers{
	scheduleTable := make(map[string] *CronJob,100)
	return &register{scheduleTable,ch}
}

func (r *register) DoRegister(job *CronJob){
	r.scheduleTable[job.name] = job
}

func (r *register) Remove(name string){
	delete(r.scheduleTable,name)
}


func (r *register) CheckAndPopup( ){
	for _, entry := range r.scheduleTable{
		now := time.Now()
		if entry.Check(now){

			copiedEntry := entry.Clone()

			r.Remove(entry.name)

			r.jobChannel <- &copiedEntry

			entry.updateNextTime(now)
			r.DoRegister(entry)
		}

	}
}
//func (r *register) Popup( ) chan CronJob{
//
//}
//

